﻿using System;
using biz.ritter.javapi.util;
using Nofs.Net.Common.Interfaces.Library;
using Nofs.Net.AnnotationDriver;


namespace Nofs.Net.nofs_addressbook
{
    [RootFolderObject]
    [DomainObject]
    [FolderObject(OperatorObject.None)] //CanAdd = false, CanRemove = false;
    public class Book
    {
        private IDomainObjectContainer<Book> _bookContainer;
        private IDomainObjectContainerManager _containerManager;


        [NeedsContainer]//@NeedsContainer
        public void setContainer(IDomainObjectContainer<Book> container)
        {
            _bookContainer = container;
        }

        [NeedsContainerManager]//@NeedsContainerManager
        public void setContainerManager(IDomainObjectContainerManager manager)
        {
            _containerManager = manager;
        }

        private IDomainObjectContainerManager GetContainerManager() //throws Exception 
        {
            if (_containerManager == null)
            {
                throw new System.Exception("container manager is null");
            }
            return _containerManager;
        }

        //@SuppressWarnings("unchecked")
        public LinkedList<Category> getCategories() //throws Exception 
        {
            var categoryContainer =
                GetContainerManager().GetContainer(typeof(Category).getClass());
            if (categoryContainer == null)
            {
                throw new System.Exception("Container for category is null");
            }

            LinkedList<Category> ret = new LinkedList<Category>();
            foreach (var item in categoryContainer.GetAllInstances())
            {
                ret.add(item as Category);
            }
            return ret;
        }


        //@SuppressWarnings("unchecked")
        public LinkedList<Contact> getContacts() //throws Exception 
        {
            IDomainObjectContainer<Contact> contactContainer =
                (GetContainerManager() as IDomainObjectContainerManager<Contact>).GetContainer(typeof(Contact).getClass());
            if (contactContainer == null)
            {
                throw new System.Exception("Container for Contact is null");
            }
            return new LinkedList<Contact>(contactContainer.GetAllInstances());
        }

        [FolderObject(OperatorObject.CanAdd | OperatorObject.CanRemove)] //@FolderObject(CanAdd=true, CanRemove=true)
        public LinkedList<Contact> getFirstTenContacts() //throws Exception 
        {
            LinkedList<Contact> firstTen = new LinkedList<Contact>();
            int count = (-1);
            foreach (Contact contact in getContacts())
            {
                if (++count == 10)
                {
                    break;
                }
                firstTen.add(contact);
            }
            return firstTen;
        }

        //@SuppressWarnings("unchecked")
        [Executable]//@Executable
        public void AddAContact(String name, String phone) //throws Exception 
        {
            var contactContainer = GetContainerManager().GetContainer(typeof(Contact).getClass());
            Contact contact = contactContainer.NewPersistentInstance() as Contact;
            contact.Name = name;
            contact.PhoneNumber = phone;
            _bookContainer.ObjectChanged(this);
        }

        //@SuppressWarnings("unchecked")
        [Executable]//@Executable
        public void RemoveAContact(Contact contact) //throws Exception 
        {
            var contactContainer = GetContainerManager().GetContainer(typeof(Contact).getClass());
            contactContainer.Remove(contact);
            _bookContainer.ObjectChanged(this);
        }

        //@SuppressWarnings("unchecked")
        [Executable]//@Executable
        public void RenameAContact(Contact contact, String newName) //throws Exception 
        {
            var contactContainer = GetContainerManager().GetContainer(typeof(Contact).getClass());
            String oldName = contact.Name;
            contact.Name = newName;
            contactContainer.ObjectRenamed(contact, oldName, newName);
            contactContainer.ObjectChanged(contact);
        }
    }

}
